﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ContactCRUD.ViewModel
{
    public class ContactViewModel : Classes.Contact
    {
        public ContactViewModel()
        {
            AddNew = new Classes.DelegateCommand<Classes.Contact>(AddNewExecute,CanAddNew);
            Reset = new Classes.DelegateCommand<Classes.Contact>(ResetExecute, CanReset);
        }

        #region Add New Command
        public Classes.DelegateCommand<Classes.Contact> AddNew { get; set; }
        
        private void AddNewExecute(Classes.Contact contact)
        {
            using (Data.TinyCRMDataBaseEntities entities = new Data.TinyCRMDataBaseEntities())
            {
                this.CompanyId = 1;
                Data.Contact dbContact = new Data.Contact();
                dbContact.Name = this.Name;
                dbContact.Surname = this.Surname;
                dbContact.Email = this.Email;
                dbContact.Phone = this.Phone;
                dbContact.dateCreated = DateTime.Now;
                dbContact.isActive = true;
                entities.AddToContacts(dbContact);
                entities.SaveChanges();
                System.Windows.MessageBox.Show(contact.Name + "Was added");
            }
            
        }
        private bool CanAddNew(Classes.Contact contact)
        {
            if (this.ErrorCollection.HasKeys())
            {
                return false;
            }
            return true;
        }
        #endregion
        #region ResetCommand 
        public Classes.DelegateCommand<Classes.Contact> Reset { get; set; }
        private void ResetExecute(Classes.Contact contact)
        {
            System.Windows.MessageBox.Show(contact.Name + "Was reseted");
        }
        private bool CanReset(Classes.Contact contact)
        {
            return !this.IsEmpty();
        }
        #endregion
        
    }
}
